/*
===============================================================================
 Name        : main.c
 Author      : 
 Version     :
 Copyright   : (C) Copyright
 Description : main definition
===============================================================================
*/

#ifdef __USE_CMSIS
#include "LPC11xx.h"
#endif

/* using general pin io */
#include "gpio/gpio.h"
/* using timer */
#include "timer/timer32.h"

/* using lcd function */
#include "lcd-api/lcd-api.h"


/*General pin id direction */
enum GPIO_DIR{
	INPUT=0,
	OUTPUT=1
};


//#define RESET_PORT (0)
//#define RESET_PIN  (9)


int main(void) {
	
	/* Basic chip initialization is taken care of in SystemInit() called
	 * from the startup code. SystemInit() and chip settings are defined
	 * in the CMSIS system_<part family>.c file.
	 */

	/*init timer 1 that using PIO1_4,TIME_INTERVAL is defined as 10mS */
	init_timer32(TIMER1, TIME_INTERVAL);

	 /* Enable timer 1. Our interrupt handler will begin incrementing
	  * the TimeTick global each time timer 0 matches and resets.
	*/
	enable_timer32(TIMER1);

	GPIOInit();

	/*------------------ RESET PIN -------------------*/
	//GPIOSetDir(RESET_PORT,RESET_PIN,OUTPUT);
	//GPIOIntDisable(RESET_PORT,RESET_PIN);

	/*------------------LCD initial-------------------*/
	/* use cs as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_CS,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_CS);
	/* clear cs = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_CS, PIN_ON );

	/* use wr as output*/
	GPIOSetDir(LCD_CONTROL_PORT,LCD_WR,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_WR);
	/* clear wr = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_WR, PIN_ON);

	/* use rs as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_RS,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_RS);
	/* clear rs = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RS, PIN_ON );

	/* use rd as output */
	GPIOSetDir(LCD_CONTROL_PORT,LCD_RD,OUTPUT);
	/* no interrupt */
	GPIOIntDisable(LCD_CONTROL_PORT, LCD_RD);
	/* clear rd = 1 */
	GPIOSetValue( LCD_CONTROL_PORT, LCD_RD, PIN_ON );

    uint32_t i;
    for(i=LCD_DATA_LOW_PORT_LSB;i<(LCD_DATA_LOW_PORT_MSB+1);i++)
    {
    	/* use lcd data low port as output */
    	GPIOSetDir(LCD_DATA_LOW_PORT,i,OUTPUT);
    	/* no interrupt */
    	GPIOIntDisable(LCD_DATA_LOW_PORT,i);
    	GPIOSetValue(LCD_DATA_LOW_PORT,i,PIN_OFF);
    }


    for(i=LCD_DATA_HIGH_PORT_LSB;i<(LCD_DATA_HIGH_PORT_MSB+1);i++)
    {
    	/* use lcd data low port as output */
    	GPIOSetDir(LCD_DATA_HIGH_PORT,i,OUTPUT);
    	/* no interrupt */
    	GPIOIntDisable(LCD_DATA_HIGH_PORT,i);
    	GPIOSetValue(LCD_DATA_HIGH_PORT,i,PIN_OFF);
    }


		uint32_t temp=0;
		i=0;

		//reset lcd first
		//GPIOSetValue(RESET_PORT,RESET_PIN,1);
		//Delayms(1);
		//GPIOSetValue(RESET_PORT,RESET_PIN,0);
		//Delayms(5);
		//GPIOSetValue(RESET_PORT,RESET_PIN,1);
		//Delayms(10);

      	init_LCD();

      	Display(0,240,320);

    	Trans_Com_16(0x0050); Trans_Dat_16(0x000F); // Horizontal GRAM Start Address
    	//Trans_Com_16(0x0051); Trans_Dat_16(0x00EF); // Horizontal GRAM End Address
    	Trans_Com_16(0x0051); Trans_Dat_16(0x002F); // Horizontal GRAM End Address
    	Trans_Com_16(0x0052); Trans_Dat_16(0x000F); // Vertical GRAM Start Address
    	//Trans_Com_16(0x0053); Trans_Dat_16(0x013F); // Vertical GRAM End Address
    	Trans_Com_16(0x0053); Trans_Dat_16(0x002F); // Vertical GRAM End Address
    	Trans_Com_16(0x0022);

      	//GPIOSetDir(0,7, 1 );
       	while(1)
        {	//GPIOSetValue(0,7, 1 );
    		//Display(colorc[temp],240,320);
       		Display(colorc[temp],0x21,0x21);
    		Delayms(1000);
    		//GPIOSetValue(0,7, 0 );
    		//Delay(3);
    		i++;
                    temp=i%9;

            /* Go to sleep to save power between timer interrupts */
           //__WFI();
         }
	return 0 ;
}
